{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "pushed = [1,2,3,4,5]\n",
    "popped = [4,5,3,2,1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "pushed = [1,2,3,4,5]\n",
    "popped = [4,3,5,1,2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "pushed = [2,1,0]\n",
    "popped = [1,2,0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 1 [2]\n",
      "1 1 [2, 1]\n",
      "0 0 [0]\n",
      "[]\n"
     ]
    }
   ],
   "source": [
    "stack = []\n",
    "k = 0\n",
    "j = 0\n",
    "times = len(pushed) * 2\n",
    "while times:\n",
    "    if k < len(pushed):\n",
    "        push = pushed[k]\n",
    "        stack.append(push)\n",
    "        k += 1\n",
    "    else:\n",
    "        if len(stack):\n",
    "            push = stack[-1]\n",
    "    if j < len(popped):\n",
    "        pop = popped[j]\n",
    "        print(push, pop, stack)\n",
    "        while pop == push:\n",
    "            stack = stack[:-1]\n",
    "            j += 1\n",
    "            if j < len(popped):\n",
    "                pop = popped[j]\n",
    "                if len(stack):\n",
    "                    push = stack[-1]\n",
    "            else:\n",
    "                break\n",
    "    times -= 1\n",
    "print(stack)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "https://leetcode.com/problems/validate-stack-sequences\n",
    "\n",
    "\n",
    "Runtime: 80 ms, faster than 25.17% of Python3 online submissions for Validate Stack Sequences.\n",
    "\n",
    "Memory Usage: 14.4 MB, less than 100.00% of Python3 online submissions for Validate Stack Sequences.\n",
    "\n",
    "\n",
    "```python\n",
    "from collections import deque\n",
    "\n",
    "class Solution:\n",
    "    def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool:\n",
    "        stack = []\n",
    "        k = 0\n",
    "        j = 0\n",
    "        times = len(pushed) * 2\n",
    "        while times:\n",
    "            if k < len(pushed):\n",
    "                push = pushed[k]\n",
    "                stack.append(push)\n",
    "                k += 1\n",
    "            else:\n",
    "                if len(stack):\n",
    "                    push = stack[-1]\n",
    "            if j < len(popped):\n",
    "                pop = popped[j]\n",
    "                #print(push, pop, stack)\n",
    "                while pop == push:\n",
    "                    stack = stack[:-1]\n",
    "                    j += 1\n",
    "                    if j < len(popped):\n",
    "                        pop = popped[j]\n",
    "                        if len(stack):\n",
    "                            push = stack[-1]\n",
    "                    else:\n",
    "                        break\n",
    "            times -= 1\n",
    "            \n",
    "        if len(stack):\n",
    "            return False\n",
    "        else:\n",
    "            return True\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
